package com.qfedu.dtboot.service.impl;

import com.qfedu.dtboot.dao.SysMenuDao;
import com.qfedu.dtboot.entity.SysMenu;
import com.qfedu.dtboot.entity.SysUser;
import com.qfedu.dtboot.dao.SysUserDao;
import com.qfedu.dtboot.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 系统用户(SysUser)表服务实现类
 *
 * @author ykx
 * @since 2022-09-27 13:02:36
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysMenuDao sysMenuDao;

    @Override
    public SysUser queryByUserName(String username) {
        return sysUserDao.queryByUserName(username);
    }

    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> perms = null;

        //超级管理员授权
        if (userId==1){
            List<SysMenu> sysMenus = sysMenuDao.queryListAll();
            perms = new ArrayList<>(sysMenus.size());
            for (SysMenu sysMenu : sysMenus) {
                perms.add(sysMenu.getPerms());
            }
        }else {
            perms = sysUserDao.queryAllPerms(userId);
        }

        //普通用户授权
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isEmpty(perm)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perm.trim().split(",")));
        }
        return permsSet;
    }
}
